package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.ctz.commonframe.entity.ImageItem;
import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.ctz.commonframe.ui.widget.ImageDisplayActivity;
import com.ctz.commonframe.utils.GlideApp;
import com.ctz.commonframe.utils.PickImageUtil;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.ctz.commonframe.ui.BottomMenuDialog;
import com.rocksdata.mc.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/4/6.
 */
@ContentView(R.layout.activity_feedback)
public class FeedbackActivity extends TitleBarActivity {
    @ViewInject(R.id.et_feedback_content)
    private EditText etContent;
    @ViewInject(R.id.gv_feedback_image)
    private GridView gvImage;
    @ViewInject(R.id.et_feedback_contact)
    private EditText etContact;

    private String content, contact;
    private BottomMenuDialog addImageDialog, handleImageDialog;
    private List<ImageItem> imageItemList = new ArrayList<>();
    private ImageGridAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleMiddleText("意见反馈");
        setTitleRightText("发送");
        setTitleRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                content = etContent.getText().toString().trim();
                contact = etContact.getText().toString().trim();
                String path = imageItemList.size() == 0 ? null : imageItemList.get(0).imagePath;
                File file = path == null ? null : new File(path);
                BusinessManager.getAccountManager().feedback(content, contact, file, new BusinessInterface.OnBusinessResultListener() {
                    @Override
                    public void onBusinessResult(boolean isSuccess, Object object, String message) {
                        showToast(message);
                    }
                });
            }
        });

        adapter = new ImageGridAdapter(this, imageItemList);
        gvImage.setAdapter(adapter);
    }

    @Event(value = R.id.gv_feedback_image, type = AdapterView.OnItemClickListener.class)
    private void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //点击了添加图片按钮
        if (imageItemList.size() < 9 && position == imageItemList.size()) {
            showAddImageDialog();
        } else {
            showHandleImageDialog(position);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        PickImageUtil.getInstance().handleActivityResult(requestCode, resultCode, data);
    }

    private void showAddImageDialog() {
        if (addImageDialog == null) {
            addImageDialog = new BottomMenuDialog(FeedbackActivity.this, BottomMenuDialog.TYPE_IMAGE_CAPTURE);
            addImageDialog.setOnMenu1ClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PickImageUtil.getInstance().selectImage(FeedbackActivity.this, new PickImageUtil.OnPickImageResultListener() {
                        @Override
                        public void onResult(List<ImageItem> imageList) {
                            handleImagePicked(imageList);
                        }
                    });
                    addImageDialog.dismiss();
                }
            });
            addImageDialog.setOnMenu2ClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PickImageUtil.getInstance().takePhoto(FeedbackActivity.this, new PickImageUtil.OnPickImageResultListener() {
                        @Override
                        public void onResult(List<ImageItem> imageList) {
                            handleImagePicked(imageList);
                        }
                    });
                    addImageDialog.dismiss();
                }
            });
        }
        addImageDialog.show();
    }

    private void handleImagePicked(List<ImageItem> list) {
        if (list != null && list.size() > 0) {
            imageItemList.set(0, list.get(0));
            adapter.notifyDataSetChanged();
        }
    }

    private void showHandleImageDialog(final int position) {
        handleImageDialog = new BottomMenuDialog(FeedbackActivity.this, BottomMenuDialog.TYPE_IMAGE_OPERATE);
        handleImageDialog.setOnMenu1ClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FeedbackActivity.this, ImageDisplayActivity.class);
                intent.putExtra("datas", (Serializable) imageItemList);
                intent.putExtra("index", position);
                startActivity(intent);
                handleImageDialog.dismiss();
            }
        });
        handleImageDialog.setOnMenu2ClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageItemList.remove(position);
                adapter.notifyDataSetChanged();
                handleImageDialog.dismiss();
            }
        });

        handleImageDialog.show();
    }

    private class ImageGridAdapter extends AbsAdapter<ImageItem> {

        public ImageGridAdapter(Context context, List<ImageItem> imageItemList) {
            super(context, imageItemList, R.layout.item_image_grid);
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public void convert(AdapterViewHolder holder, ImageItem data) {
            ImageView imageView = holder.getView(R.id.image);
            GlideApp.with(context).clear(imageView);
            if (imageItemList.size() == 0) {
                imageView.setImageResource(R.mipmap.ic_add_image);
            } else {
                if (data.thumbnailPath != null) {
                    GlideApp.with(context).load(data.thumbnailPath).into(imageView);
                } else if (data.imagePath != null) {
                    GlideApp.with(context).load(data.imagePath).into(imageView);
                }
            }
        }
    }
}
